package com.example.distinguish_server.service.impl;

import com.alibaba.fastjson.JSON;
import com.example.distinguish_server.entity.PlateNumber;
import com.example.distinguish_server.entity.prop.JsonRootBean;
import com.example.distinguish_server.entity.prop.Vertexes_location;
import com.example.distinguish_server.entity.prop.Words_result;
import com.example.distinguish_server.mapper.PlateNumberMapper;
import com.example.distinguish_server.service.PlateNumberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestPart;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xg
 * @since 2023-09-20
 */
@Service
public class PlateNumberServiceImpl extends ServiceImpl<PlateNumberMapper, PlateNumber> implements PlateNumberService {
    @Resource
    private PlateNumberMapper plateNumberMapper;

    @Override
    public boolean addPlateNumber(JsonRootBean jsonRootBean) {
        List<PlateNumber> plateNumbers = new ArrayList<>();
        List<Words_result> words_result = jsonRootBean.getWords_result();
        for (Words_result wordsResult : words_result) {
            if(wordsResult == null){
                continue;
            }
            String color = wordsResult.getColor();
            List<Double> probability = wordsResult.getProbability();
            String number = wordsResult.getNumber();
            List<Vertexes_location> vertexesLocation = wordsResult.getVertexes_location();

            String jsonStr = JSON.toJSONString(vertexesLocation);
            String probabilityStr = String.join(",", probability.toString());

            PlateNumber plateNumber = new PlateNumber();
            plateNumber.setLogId(jsonRootBean.getLog_id());
            plateNumber.setColor(color);
            plateNumber.setProbability(probabilityStr);
            plateNumber.setNumber(number);
            plateNumber.setVertexesLocation(jsonStr);
//            boolean b = this.save(plateNumber);

            plateNumbers.add(plateNumber);

        }
        if(plateNumbers == null || plateNumbers.size() == 0){
            return false;
        }

        System.out.println(plateNumbers.size());
        boolean b = this.insertBatch(plateNumbers);
        return b;
    }

    @Override
    public boolean insertBatch(List<PlateNumber> plateNumbers) {
        boolean b = plateNumberMapper.insertBatch(plateNumbers);
        return b;
    }
}
